package liujia.bt.display.base
{
	import liujia.bt.display.core.BtRun;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	
	/**
	 * Canvas
	 * @author 刘佳 liujia08@snda.com
	 * $Id: BtCanvas.as 2 2011-10-31 01:46:19Z sigmaliu@163.com $
	 * @version 1.0
	 */
	public class BtCanvas extends Bitmap
	{
		private var bmpVec:Vector.<BtCanvasChild> = new Vector.<BtCanvasChild>;
		
		private var rect:Rectangle;
		
		public function BtCanvas(w:Number, h:Number)
		{
			super(new BitmapData(w, h, true, 0));
			rect = new Rectangle(0, 0, w, h);
			BtRun.stage.addEventListener(Event.ENTER_FRAME, onFrame);
		}
		
		public function addBmp(bmp:BtCanvasChild):void {
			bmpVec.push(bmp);
		}
		
		public function removeBmp(bmp:BtCanvasChild):void {
			var index:int = bmpVec.indexOf(bmp);
			if (index != -1) {
				bmpVec[index] = bmpVec.pop();
			}
		}
		
		private function onFrame(e:Event):void {
			bitmapData.lock();
			bitmapData.fillRect(rect, 0);
			for each (var bmp:BtCanvasChild in bmpVec) {
				bitmapData.copyPixels(bmp.bitmapdata, bmp.sourceRect, bmp.destPoint, null, null, true);
			}
			bitmapData.unlock();
		}
			
	}
}
